using System;
using System.Text.RegularExpressions;

namespace newtelligence.DasBlog.Web.Core
{
    internal class Util
    {
        /// <summary>
        /// Replace something in a expression via a regular expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="replaceRegex">The regular expression.</param>
        /// <param name="replaceExpression">The replacement.</param>
        /// <returns>A string. If the regular expression does'nt match, the string is the expression.</returns>
        public static String ReplaceViaRegEx(string expression, string replaceRegex, string replaceExpression)
        {
            if (!String.IsNullOrEmpty(replaceRegex))
            {
                return Regex.Replace(expression,
                                     replaceRegex,
                                     replaceExpression,
                                     RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
            }
            else
            {
                return expression;
            }
        }

        public static string TruncateString(string s, int max)
        {
            if (s == null)
            {
                return "";
            }
            else if (s.Length < max)
            {
                return s;
            }
            else if (s.Length >= max)
            {
                return s.Substring(0, max) + "...";
            }

            return s;
        }
    }
}